package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.InviteRankingAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.InviteBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SpannableStringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 邀请好友
 */
public class InviteFriendActivity extends BaseActivity {

    @BindView(R.id.tv_my_fencheng)
    TextView tvMyFencheng;
    @BindView(R.id.tv_my_invite)
    TextView tvMyInvite;
    @BindView(R.id.tv_my_renmai)
    TextView tvMyRenmai;
    @BindView(R.id.tv_my_invite_details)
    TextView tvMyInviteDetails;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.tv_share_qrcode)
    TextView tvShareQrcode;
    @BindView(R.id.tv_invite_ranking)
    TextView tvInviteRanking;
    @BindView(R.id.tv_my_invite_ranking)
    TextView tvMyInviteRanking;
    @BindView(R.id.rv_ranking)
    RecyclerView rvRanking;

    private List<InviteBean.ListBean> rankingData;
    private InviteRankingAdapter adapter;
    private int type = 1;//1奖励排行榜 2我的邀请成绩
    private int page = 1;//分页,当type=2时有分页
    private String myJaingli;

    public static void actionStar(Context context){
        Intent intent = new Intent(context,InviteFriendActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_invite_friend;
    }

    @Override
    protected void initView() {
        setTitleText("邀请好友");

        initRecyclerView();
        getData();
    }

    private void getData() {
        Map map = new HashMap();
        map.put("type",type+"");
        if(type == 2){
            map.put("page",page+"");
        }
        OkGoUtil.postRequest(getContext(), UrlConstants.MY_INVITE, map, new JsonCallback<BaseModel<InviteBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<InviteBean>> response) {
                if (response != null && response.body() != null) {
                    rankingData.clear();
                    rankingData.addAll(response.body().getData().getList());
                    adapter.notifyDataSetChanged();

                    setView(response.body().getData());
                }else{
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<InviteBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void initRecyclerView() {
        rankingData = new ArrayList<>();
        adapter = new InviteRankingAdapter();
        rvRanking.setLayoutManager(new LinearLayoutManager(context));
        rvRanking.setAdapter(adapter);
        adapter.setNewData(rankingData);
    }

    @Override
    protected void initData() {

    }

    private void setView(InviteBean data) {
        myJaingli = data.getData().getMy_commission()+"";
        tvMyFencheng.setText(SpannableStringUtils.getInstance().getLastSizeChangeText(data.getData().getMy_commission()+"元",1));
        tvMyInvite.setText(SpannableStringUtils.getInstance().getLastSizeChangeText(data.getData().getOne()+"人",1));
        tvMyRenmai.setText(SpannableStringUtils.getInstance().getLastSizeChangeText(data.getData().getTwo()+"人",1));
    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    @OnClick({R.id.tv_my_invite_details, R.id.tv_invite, R.id.tv_share_qrcode, R.id.tv_invite_ranking, R.id.tv_my_invite_ranking})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_my_invite_details:
                InviteDetailsActivity.actionStar(context,myJaingli);
                break;
            case R.id.tv_invite:
                break;
            case R.id.tv_share_qrcode:
                break;
            case R.id.tv_invite_ranking:
                if(type != 1){
                    type = 1;
                    getData();
                    tvInviteRanking.setTextColor(getResources().getColor(R.color.color_333));
                    tvMyInviteRanking.setTextColor(getResources().getColor(R.color.gray9));
                }
                break;
            case R.id.tv_my_invite_ranking:
                if(type != 2){
                    type = 2;
                    getData();
                    tvInviteRanking.setTextColor(getResources().getColor(R.color.gray9));
                    tvMyInviteRanking.setTextColor(getResources().getColor(R.color.color_333));
                }
                break;
        }
    }
}